//
//  KindsViewController.m
//  MarianasTrench
//
//  Created by wangjue on 16/3/17.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "KindsViewController.h"

@interface KindsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *kindCollection;/**< 主页面Collection >*/
@property (nonatomic, strong) NSMutableArray *arrModel;/**< model数组 >*/
@property (nonatomic, strong) NSMutableArray *headerArr;/**< 头视图数组 >*/
@property (nonatomic, strong) UICollectionReusableView *footer;
@property (nonatomic, strong) NSMutableArray *scrollArr;
@property (nonatomic, strong) DCPicScrollView *picView;

@end

@implementation KindsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.arrModel = [NSMutableArray array];
    self.headerArr = [NSMutableArray array];
    self.scrollArr = [NSMutableArray array];
    self.navigationController.navigationBar.translucent = NO;
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    title.text = @"马里亚纳大海🐶";
    title.textColor = [UIColor redColor];
    title.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = title;
    
    [self dataHandel];
    [self scrollDataHandel];
    [self createSubViews];
}


#pragma mark - 创建子视图
- (void)createSubViews {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 2, 70);
    
    self.kindCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HETGHT - 113) collectionViewLayout:layout];
    [self.view addSubview:_kindCollection];
    _kindCollection.delegate = self;
    _kindCollection.dataSource = self;
    _kindCollection.backgroundColor = [UIColor whiteColor];
    [_kindCollection registerClass:[KindCollectionViewCell class] forCellWithReuseIdentifier:@"KindCollectionViewCell"];
    [_kindCollection registerClass:[KindReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"KindReusableView"];
    [_kindCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];
    
}

#pragma mark - 返回Cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrModel.count;
}

#pragma mark - 创建Cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    KindCollectionViewCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KindCollectionViewCell" forIndexPath:indexPath];
    
    KindModel *model = _arrModel[indexPath.row];
    Cell.kindModel = model;
    Cell.backgroundColor = [UIColor whiteColor];
    
    return Cell;
}

#pragma mark - 创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        KindReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"KindReusableView" forIndexPath:indexPath];
        if (_headerArr.count > 0) {
            /** 有声书 */
            [header.bookButton BUTTONHANDLEIMAGE(_headerArr[0][@"coverPath"])];
            [header.bookButton addTarget:self action:@selector(bookButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            /** 音乐 */
            [header.musicButton BUTTONHANDLEIMAGE(_headerArr[1][@"coverPath"])];
            [header.musicButton addTarget:self action:@selector(musicButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            /** 相声评书 */
            [header.crosstalkButton BUTTONHANDLEIMAGE(_headerArr[3][@"coverPath"])];
            [header.crosstalkButton addTarget:self action:@selector(crosstalkButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            /** 娱乐 */
            [header.amuseButton BUTTONHANDLEIMAGE(_headerArr[2][@"coverPath"])];
            [header.amuseButton addTarget:self action:@selector(amuseButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            /** 儿童 */
            [header.childButton BUTTONHANDLEIMAGE(_headerArr[4][@"coverPath"])];
            [header.childButton addTarget:self action:@selector(childButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        return header;
    }
    if (kind == UICollectionElementKindSectionFooter) {
        self.footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Footer" forIndexPath:indexPath];
        return _footer;
    }
    return nil;
}

#pragma mark - button点击方法
- (void)bookButtonAction:(UIButton *)button {
    DetailKindController *detailVc = [[DetailKindController alloc] init];
    detailVc.httpId = @"3";
    detailVc.titleName = @"有声书";
    [self.navigationController pushViewController:detailVc animated:YES];
}

- (void)musicButtonAction:(UIButton *)button {
    DetailKindController *detailVc = [[DetailKindController alloc] init];
    detailVc.httpId = @"2";
    detailVc.titleName = @"音乐";
    [self.navigationController pushViewController:detailVc animated:YES];
}

- (void)crosstalkButtonAction:(UIButton *)button {
    DetailKindController *detailVc = [[DetailKindController alloc] init];
    detailVc.httpId = @"4";
    detailVc.titleName = @"相声评书";
    [self.navigationController pushViewController:detailVc animated:YES];
}

- (void)amuseButtonAction:(UIButton *)button {
    DetailKindController *detailVc = [[DetailKindController alloc] init];
    detailVc.httpId = @"12";
    detailVc.titleName = @"娱乐";
    [self.navigationController pushViewController:detailVc animated:YES];
}

- (void)childButtonAction:(UIButton *)button {
    DetailKindController *detailVc = [[DetailKindController alloc] init];
    detailVc.httpId = @"6";
    detailVc.titleName = @"儿童";
    [self.navigationController pushViewController:detailVc animated:YES];
}

#pragma mark - collectionView头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH, SCREEN_HETGHT / 7 * 2);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH, 70);
}

#pragma mark - CollectionViewCell点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    KindModel *model = _arrModel[indexPath.row];
    DetailKindController *detailVc = [[DetailKindController alloc] init];
    detailVc.httpId = model.httpId;
    detailVc.titleName = model.title;
    [self.navigationController pushViewController:detailVc animated:YES];
    
}

#pragma mark - 数据解析
- (void)dataHandel {
    
    NSString *httpStr = @"http://mobile.ximalaya.com/mobile/discovery/v1/categories?device=iPhone&picVersion=11&scale=2";
    [APPTools GETWithURL:httpStr par:nil success:^(id responseObject) {
        
        for (NSDictionary *dic in responseObject[@"list"]) {
            if ([dic[@"orderNum"] integerValue] <= 5) {
                [self.headerArr addObject:dic];
            }
            else {
                KindModel *model = [[KindModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.arrModel addObject:model];
            }
        }
        
        [_kindCollection reloadData];
        
    } filed:^(NSError *error) {
        
    }];
    
}

- (void)scrollDataHandel {
    NSString *http = @"http://adse.ximalaya.com/ting?appid=0&device=iPhone&name=cata_index_banner&network=WIFI&operator=3&scale=2&version=4.3.26";
    [APPTools GETWithURL:http par:nil success:^(id responseObject) {
        
        for (NSDictionary *dic in responseObject[@"data"]) {
            [self.scrollArr addObject:dic];
        }
        
        [self createScroller];
        
    } filed:^(NSError *error) {
        
    }];
}

- (void)createScroller {
    NSMutableArray *tempArr = [NSMutableArray array];
    for (NSDictionary *dic in _scrollArr) {
        [tempArr addObject:dic[@"cover"]];
    }
    NSArray *UrlStringArray = [NSArray arrayWithArray:tempArr];
    self.picView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 70) WithImageUrls:UrlStringArray];
    [self.footer addSubview:_picView];
    _picView.AutoScrollDelay = 3;
    [[DCWebImageManager shareManager] setDownloadImageRepeatCount:1];
    [[DCWebImageManager shareManager] setDownLoadImageError:^(NSError *error, NSString *string) {
        
    }];
    _picView.imageViewDidTapAtIndex = ^(NSInteger index) {
        
        
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
